<?php

namespace app\index\controller;

use app\index\controller\Base;
use think\Request;
use think\Db;

class Message extends Base
{
    public function index(Request $request)
    {

        //每页显示多少条
        $limit = $this->systemConfig['other_setting']['message_page'];
        //当前是第几页
        $page = 1;


        $map['messagetable_id'] = $request->isGet() && $request->has('message_id') ? $request->param('message_id') : 0;
        $map['messagetable_type'] = $request->isGet() && $request->has('message_type') ? $request->param('message_type') : 0;

        //是否是分页跳转  每页显示多少条
        $limit = $request->isGet() && $request->has('limit') ? $request->param('limit') : $limit;
        //是否有参数  当前是第几页
        $page = $request->isGet() && $request->has('page') ? $request->param('page') : $page;


        $data = Db::name($request->controller())->where($map)->order('id', 'desc')->paginate($limit, FALSE, [
            'page' => $page,
            'query' => [$map],
        ])->toArray();

        $data['limit'] = $limit;
//        halt($data);
        $this->assign(['data' => $data]);

        return $this->fetch();
    }

    //增加
    public function store(Request $request)
    {
        $data['avatar'] = $request->has('avatar')?$request->param('avatar'):'/static/index/img/avatar.jpg';
        $data['name'] = $request->param('name');
        $data['content'] = $request->param('content');
        $data['create_time'] = strtotime($request->param('time'));
        $data['messagetable_id'] = $request->param('messagetable_id');
        $data['messagetable_type'] = $request->param('messagetable_type');
//        halt($data);
        $rel = Db::name($request->controller())->insert($data);

        if ($rel !== false){
            return return_json(0,'留言成功');
        }
        return return_json(1,'留言失败');
    }
}
